/**
 * Created by seeker910 on 2017/6/11.
 * 包含：日期和时间
 * 输入值为 int型
 * 返回 类型与输入值类型一致，为 int型。
 * 该控件用于 对timestamp值 使用用int 类型存储对数据
 */
Rsd.define('Rsd.form.Timestamp', {
    extend: 'Rsd.control.Component',
    xtype: 'timestamp',
    ctrlTagName: 'div',
    /**
     * html 时间控件不支持到毫秒级
     */
    formatString:'yyyy-MM-dd hh:mm:ss',
    defaultTime:'00:00:00',
    precision:3,
    height:30,
    width:320,
    /**
     * 
     * @param {*} config 
     */
    constructor: function Timestamp(config) {
        config = config || {};
        this.apply(config);
    },

    /***
     * 
     */
    initComponentEx: function initComponentEx() {
        var me = this;
        this.callParent();
        var height = "86%";//calc("+this.height+"-4px)
        
        this.___date = document.createElement('input');
        this.___date.type = "date";
        this.___date.style.width = "48%";
        this.___date.style.height = height;

        if(me.formatString&&me.formatString.toLowerCase().startWith('yyyy-mm-dd'))
        {
            this.ctrl.element.appendChild(this.___date);
        }
       
        this.___time = document.createElement('input');
        this.___time.type = "time";
        this.___time.step = 1;
        this.___time.style.width = "48%";
        this.___time.style.height = height;
        this.___time.style.float = "right";

        if(me.formatString&&me.formatString.toLowerCase().indexOf('hh:mm:ss')>=0)
        {
            this.ctrl.element.appendChild(this.___time);
        }
        
    },
     /*
     * */
    onAfterInit: function onAfterInit() {
        this.callParent();
        var me = this;
        me.dom.style.textAlign = 'left'; 
        me.ctrl.element.name = me.dataIndex;

        me.___date.readonly = me.readonly;
        me.___date.disabled = me.disabled;
        me.___date.required = me.required?'required':'';
        
        me.___date.autocomplete="off";
        me.___date.accessKey = me.accessKey;
        me.___date.onchange = function (evt) {

            me.funApplyByIOC(me.onchange||me.textChanged,[me,evt]);
        }
    
        me.___time.readonly = me.readonly;
        me.___time.disabled = me.disabled;
        me.___time.required = me.required?'required':'';
    
        me.___time.autocomplete="off";
        me.___time.accessKey = me.accessKey;
        me.___time.onchange = function (evt) {

            me.funApplyByIOC(me.onchange||me.textChanged,[me,evt]);
        }

        if (me.__value != undefined) {
             
            me.setValue(me.__value) ;
        }      
    
   },
     /*
    * */
     setDisabled: function setDisabled(disabled) {
        this.callParent(disabled);
        if( this.___date)
        {
            this.___date.disabled = this.disabled;
            this.___time.disabled = this.disabled;
        }

    },
    /***
     *  重写setValue
     * 因为继承了Rsd.form.Date 所以 this.__value 的实际指类型必须是Date
     */
    setValue:function setValue(value)
    {
        if( this.__value == value)
        {
            return;
        }
        
        var _date = null;
        if(value == null)
        {
            _date = new Date(0);
        }
       
        if(value instanceof Date)
        {
            _date = value;
        }

        if(_date==null && !Rsd.isNumber(value))
        {
            Rsd.error('Timestamp值设置错误：【'+ value + '】不是有效的数字类型。');
            return;
        }

        if(_date == null)
        {
            var _value = parseInt(value);
            for(var i = _value.toString().length ;i < 13;i++)
            {
                _value = _value * 10;
            }
             _date = new Date(_value) ;
        }
      
         this.__value = _date;

        if (this.___date) {

            if( _date != null && _date > 0)
            {
                this.___date.value = _date.format('yyyy-MM-dd');
                this.___time.value = _date.format('hh:mm:ss');
             
            }
            else
            {
                this.___date.value = "";
                this.___time.value = "";
            }
            
        }

    },
    /**
     * 重写getValue
     * 无效时间值，返回null.
     */
    getValue:function getValue() {
 
        //js 能表现出来的值 是13位长度
        //console.log('date:', this.___date.value);
        //console.log('time:', this.___time.value);
        var str = this.___date.value + ' ' + (this.___time.value||this.defaultTime||'00:00:00');
        //console.log(str);
        var _value = Date.parse(str) ;
    
        if(isNaN(_value))
        {
            return 0;
        }

        if(this.precision > 3)
        {
            for(var i = 0 ;i < (this.precision - 3);i++)
            {
                    _value = _value * 10;
            }
        }
        if(this.precision < 3)
        {
            for(var i = 0 ;i < (3-this.precision );i++)
        {
                _value = _value / 10;
        }
        }
        
        return _value;
    }

});